package com.adcok.lcdns.activity;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.adcok.lcdns.util.ApplicationUtils;
import com.adcok.lcdns.util.CNCronosLog;
import com.adcok.lcdns.R;

/**
 * 약관동의 Activity
 * @author Administrator
 *
 */
public class LottoAdAgreeActivity extends Activity{
	
	private Context mContext;
	
	/**동의 Button*/
	private Button mAgreeBtn;			
	
	/**동의 CheckBox*/
	private CheckBox mAgreeChk;
	
	private TextView mAgreeTv;
	
	/**CheckBox 체크 유,무*/
	private boolean mIsChecked = false;
	
	private String mTerm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.mContext = this;
		
		setContentView(R.layout.lottoad_agree);
		
		mAgreeBtn = (Button) findViewById(R.id.button_agree);
		mAgreeChk = (CheckBox) findViewById(R.id.check_agree);
		
		mAgreeTv = (TextView) findViewById(R.id.textView_agree);
		
		mAgreeChk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				mIsChecked = isChecked;
			}
		});
		
		mAgreeBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mIsChecked){
					setResult(RESULT_OK);
					finish();
				} else {
					ApplicationUtils.showOkDialog(mContext, 0, getString(R.string.agree_title), getString(R.string.agree_contents));
				}
			}
		});
		
		try {
			mAgreeTv.setText(readText("term.txt"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	 private String readText(String file) throws IOException {
		  InputStream is = getAssets().open(file);

		  int size = is.available();
		  byte[] buffer = new byte[size];
		  is.read(buffer);
		  is.close();

		  String text = new String(buffer);
		  CNCronosLog.d("readText==========>"+text);
		  
		  mTerm = text;
		  
		  return mTerm;
	 }
}
